﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Z2102WinForms.LX
{
    public partial class Form6 : Form
    {
        public Form6()
        {
            InitializeComponent();

            label1.Text = "上机练习6 数组排序和计算（要求分别用Console和WinForms实现）。";
            label2.AutoEllipsis = true;
            button1.Click += Button1_Click;

            panel1.Visible = false;
            buttonConsole.Click += delegate
            {
                Program.ExecConsoleApp("Z2102Console.LX.Class6");
            };
            buttonWinForms.Click += delegate
            {
                panel1.Visible = true;
            };
        }

        private void Button1_Click(object? sender, EventArgs e)
        {
            string s = textBox1.Text;
            if (s.Length == 0)
            {
                this.Close();
                return;
            }
            string[] sArray = s.Split(' ');
            if (sArray.Length != 5)
            {
                label2.Text = "错误：输入的不是5个数，请重新输入。";
                return;
            }
            bool isValid = true;
            int[] nArray = new int[5];
            for (int i = 0; i < nArray.Length; i++)
            {
                if (int.TryParse(sArray[i], out nArray[i]) == false)
                {
                    label2.Text = $"错误：无法将\"{sArray[i]}\"转换为整数，请重新输入。";
                    isValid = false;
                }
            }
            if (!isValid)
            {
                return;
            }
            s = "";
            Array.Sort(nArray);
            s += string.Format("正序：{0}\r\n", string.Join(",", nArray));
            Array.Reverse(nArray);
            s += string.Format("逆序：{0}\r\n", string.Join(",", nArray));
            s += string.Format("平均值：{0:f1}\r\n", nArray.Average());
            s += string.Format("最大值：{0:f1}\r\n", nArray.Max());
            label2.Text = s;
        }

        private void Form6_Load(object sender, EventArgs e)
        {

        }
    }
}
